package com.question.mapper;

import com.question.pojo.entity.Category;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface CategoryMapper {

    /**
     * 获取所有分类
     *
     * @return 所有分类
     */
    @Select("SELECT category_id, major_id, category_name, description FROM question_categories")
    List<Category> selectAllCategories();

    // 新增
    @Insert("INSERT INTO question_categories (major_id, category_name, description) " +
            "VALUES (#{majorId}, #{categoryName}, #{description})")
    @Options(useGeneratedKeys = true, keyProperty = "categoryId")
    void insertCategory(Category category);

    // 删除
    @Delete("DELETE FROM question_categories WHERE category_id = #{categoryId}")
    int deleteCategory(Integer categoryId);

    // 更新
    @Update("UPDATE question_categories SET major_id = #{majorId}, category_name = #{categoryName}, " +
            "description = #{description} WHERE category_id = #{categoryId}")
    int updateCategory(Category category);

    // 根据ID查询
    @Select("SELECT category_id, major_id, category_name, description " +
            "FROM question_categories WHERE category_id = #{categoryId}")
    Category getCategoryById(Integer categoryId);

    // 根据专业统计分类数
    @Select("SELECT COUNT(*) FROM question_categories WHERE major_id = #{majorId}")
    int countByMajorId(Integer majorId);

    // 根据专业ID查询分类
    @Select("SELECT category_id, category_name, description " +
            "FROM question_categories WHERE major_id = #{majorId}")
    List<Category> selectCategoriesByMajorId(Integer majorId);
}